﻿using RRQMSocket;
using RRQMSocket.FileTransfer;
using System.Diagnostics;
using System.Timers;
using System.Windows;
using System.Windows.Controls;

namespace Views
{
    /// <summary>
    /// TransferFilePanel.xaml 的交互逻辑
    /// </summary>
    public partial class SelfTransferFilePanel : UserControl
    {
        public SelfTransferFilePanel()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set { SetValue(FileNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(SelfTransferFilePanel), new PropertyMetadata("1.txt"));

        public string FileLength
        {
            get { return (string)GetValue(FileLengthProperty); }
            set { SetValue(FileLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FileLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FileLengthProperty =
            DependencyProperty.Register("FileLength", typeof(string), typeof(SelfTransferFilePanel), new PropertyMetadata("10Mb"));

        public string FilePath
        {
            get { return (string)GetValue(FilePathProperty); }
            set { SetValue(FilePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FilePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FilePathProperty =
            DependencyProperty.Register("FilePath", typeof(string), typeof(SelfTransferFilePanel), new PropertyMetadata("1.txt"));

        public double UploadProgressValue
        {
            get { return (double)GetValue(UploadProgressValueProperty); }
            set { SetValue(UploadProgressValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UploadProgressValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UploadProgressValueProperty =
            DependencyProperty.Register("UploadProgressValue", typeof(double), typeof(SelfTransferFilePanel), new PropertyMetadata(0.0));

        public string SendSpeed
        {
            get { return (string)GetValue(SendSpeedProperty); }
            set { SetValue(SendSpeedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SendSpeed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SendSpeedProperty =
            DependencyProperty.Register("SendSpeed", typeof(string), typeof(SelfTransferFilePanel), new PropertyMetadata(0.0));

        private Timer timer;
        private FileClient fileClient;

        public void SendFile(string targetName)
        {
            fileClient = new FileClient();

            ConnectSetting connectSetting = new ConnectSetting();

            fileClient.UploadFileFinshed += FileClient_UploadFileFinshed;

            try
            {
                fileClient.Connect(connectSetting);
                FileUrl url = new FileUrl();
                url.FilePath = this.FilePath;
                fileClient.UploadFile(url);

                timer = new Timer(1000);
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            }
            catch (System.Exception)
            {
            }
        }

        private void FileClient_UploadFileFinshed(object sender, FileFinishedArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                timer.Stop();
                timer.Dispose();

                this.UploadProgressValue = 1;
                this.SendSpeed = string.Empty;
                // this.Text = "上传完成";
            });
        }

        private void Button_OpenFile(object sender, RoutedEventArgs e)
        {
            Process.Start(this.FilePath);
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.UploadProgressValue = fileClient.UploadProgressValue;
                this.SendSpeed = string.Format("{0}Kb/s", fileClient.UploadSpeed.ToString("0.00"));
            });
        }
    }
}